//
//  WebViewController.m
//  MiBaoTu
//
//  Created by admin on 16/11/3.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "WebViewController.h"
#import "MBTabBarController.h"
#import "AppDelegate.h"

@interface WebViewController ()<UIWebViewDelegate> {
    UIWebView               *_webView;
}

@end
@implementation WebViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //set up same item by you
    [self onBackItem];
    
    [self onCreateUI];
}

- (void)onBackItem
{
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    navView.backgroundColor = TINK_COLOR;
    [self.view addSubview:navView];
    
    UIImage *img = [UIImage imageNamed:@"fanhui"];
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(10, 20+(44-img.size.height)/2., 80, img.size.height);
    [back setImage:img forState:UIControlStateNormal];
    [back setImage:img forState:UIControlStateHighlighted];
    [back setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 80-img.size.width)];
    [back addTarget:self action:@selector(onBackForBt) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:back];
    
    UILabel *myTitleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 20+7, SCREEN_WIDTH, 30)];
    myTitleLb.text = @"";
    myTitleLb.textAlignment = NSTextAlignmentCenter;
    myTitleLb.textColor = [UIColor whiteColor];
    [navView addSubview:myTitleLb];

}

- (void)onCreateUI
{
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    _webView.backgroundColor = [UIColor clearColor];
    _webView.delegate = self;
    [self.view addSubview:_webView];
    
    NSString *url = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"ForwardUrl"]];
//    url = @"http://www.baidu.com";
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [_webView loadRequest:request];
}

#pragma mark - actions
- (void)onBackForBt
{
    MBTabBarController *tabBar = [[MBTabBarController alloc] init];
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.window.rootViewController = tabBar;
}

#pragma mark - webView
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [MBProgressHUD showHUDAddedTo:_webView animated:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [MBProgressHUD hideHUDForView:_webView animated:YES];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:( NSError *)error
{
    [MBProgressHUD hideHUDForView:_webView animated:YES];
}

@end
